﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.Administration.Engine;
using HerGoS.Administration.Entities;
using System.Data;
using HerGoS.WebControlExtend;
using HerGoS.ControlsUI;

namespace HerGoS.WebUI
{
    public partial class TipoDocumento_Listado : PageBase
    {
        #region Propiedades
        public List<TipoDocumento> TiposDocumentos
        {
            get
            {
                if (ViewState["tiposdocumentos"] != null)
                    return (List<TipoDocumento>)ViewState["tiposdocumentos"];
                return null;
            }
            set
            {
                ViewState["tiposdocumentos"] = value;
            }

        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Initialize();
                LoadGridTipoDocumento(false);
            }
        }

        void Initialize()
        {
            this.GridTipoDocumento.Settings(new GridViewExtBasic());
            this.btnNuevo.Settings(new ButtonExtText());
            this.btnRefresh.Settings(new ButtonExtText());                        
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadGridTipoDocumento(false);
        }

        protected void btnBorrar_Click(object sender, EventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int index = Convert.ToInt32(btn.CommandArgument);
            int id = Convert.ToInt32(GridTipoDocumento.DataKeys[index].Value);

            //TipoDocumento tipoDocumento = ((List<TipoDocumento>)this.GridTipoDocumento.DataSource).Find(c => c.OID == id);
            TipoDocumento tipoDocumento = TiposDocumentos.Find(c => c.OID == id);
            TiposDocumentos.Remove(tipoDocumento);

            GestorTipoDocumento.Eliminar(tipoDocumento);
            LoadGridTipoDocumento(true);

        }

        private List<TipoDocumento> Filter()
        {

            string codigo = "";
            string descripcion = "";
            try
            {
                codigo = ((TextBox)GridTipoDocumento.HeaderRow.FindControl("txtCodigo")).Text;
                descripcion = ((TextBox)GridTipoDocumento.HeaderRow.FindControl("txtDescripcion")).Text;
            }
            catch (Exception)
            { }

            return TiposDocumentos.Where(x => x.Codigo.ToLower().StartsWith(codigo.ToLower())
                && x.Descripcion.ToLower().StartsWith(descripcion.ToLower())).ToList();
        }

        private void LoadGridTipoDocumento(bool cache)
        {
            if (!cache)
                TiposDocumentos = GestorTipoDocumento.Consultar();

            this.GridTipoDocumento.DataSource = TiposDocumentos;
            this.GridTipoDocumento.DataBind();
        }

        protected void GridTipoDocumento_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButtonExt Editar = e.Row.FindControl("btnEditar") as ImageButtonExt;
                if (Editar != null)
                {
                    Editar.Settings(new ImageButtonExtEdit());
                    int oid = DataBinder.Eval(e.Row.DataItem, "OID").ToInteger();
                    Editar.OnClientClick = "Editar(" + oid.ToString() + "); return false;";
                }

                ImageButtonExt Borrar = e.Row.FindControl("btnBorrar") as ImageButtonExt;
                if (Borrar != null)
                {
                    Borrar.Settings(new ImageButtonExtDelete());
                }

            }
        }

        protected void GridTipoDocumento_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            GridTipoDocumento.PageIndex = e.NewPageIndex;
            LoadGridTipoDocumento(true);
        }


    }

}
